package com.aispeech.testcustom.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.aispeech.testcustom.R;
import com.aispeech.testcustom.utils.UiUtils;

public class FreakActivity extends AppCompatActivity {

    private Button mBtnRgt;
    private EditText mEtInput1;
    private EditText mEtInput2;
    private EditText mEtInput3;
    private EditText mEtInput4;
    private EditText mEtInput5;
    private EditText mEtInput6;
    private EditText mEtInput7;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            FreakActivity.this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_freak);

        UiUtils.showBack4ActionBar(this,"奇葩测试");

        initView();
        mBtnRgt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] inputs = getInput();

                FreakActivity.this.setResult(RESULT_OK,FreakActivity.this.getIntent().putExtra("getInput",inputs));
                FreakActivity.this.finish();
            }
        });
    }

    //获取输入的内容,
    private String[] getInput() {
        String[] inputs = new String[7];
        String s1 = mEtInput1.getText().toString();
        String s2 = mEtInput2.getText().toString();
        String s3 = mEtInput3.getText().toString();
        String s4 = mEtInput4.getText().toString();
        String s5 = mEtInput5.getText().toString();
        String s6 = mEtInput6.getText().toString();
        String s7 = mEtInput7.getText().toString();

        if (!TextUtils.isEmpty(s1)){
            inputs[0]=s1;
        }
        if (!TextUtils.isEmpty(s2)){
            inputs[1]=s2;
        }
        if (!TextUtils.isEmpty(s3)){
            inputs[2]=s3;
        }
        if (!TextUtils.isEmpty(s4)){
            inputs[3]=s4;
        }
        if (!TextUtils.isEmpty(s5)){
            inputs[4]=s5;
        }
        if (!TextUtils.isEmpty(s6)){
            inputs[5]=s6;
        }
        if (!TextUtils.isEmpty(s7)){
            inputs[6]=s7;
        }

        //去掉数组中的空元素
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<inputs.length; i++) {
            if("".equals(inputs[i])||inputs[i]==null) {
                System.out.print(i + " ============-=-=-==");
                continue;
            }
            sb.append(inputs[i]);
            if(i != inputs.length - 1) {
                sb.append(";");
            }
        }

        inputs = sb.toString().split(";");

        //打印数组
        for(int i=0; i<inputs.length; i++) {
            System.out.print(inputs[i] + " ");
        }

        return inputs;
    }

    private void initView() {
        mBtnRgt = (Button) findViewById(R.id.freak_btn_register);
        mEtInput1 = (EditText) findViewById(R.id.freak_et_input1);
        mEtInput2 = (EditText) findViewById(R.id.freak_et_input2);
        mEtInput3 = (EditText) findViewById(R.id.freak_et_input3);
        mEtInput4 = (EditText) findViewById(R.id.freak_et_input4);
        mEtInput5 = (EditText) findViewById(R.id.freak_et_input5);
        mEtInput6 = (EditText) findViewById(R.id.freak_et_input6);
        mEtInput7 = (EditText) findViewById(R.id.freak_et_input7);
    }
}
